package com.example.mymvp_2.Retrofit02.Presenter;

import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.example.mymvp_2.Base.BasePressenter;
import com.example.mymvp_2.Base.BaseView;
import com.example.mymvp_2.Bean.Channel;
import com.example.mymvp_2.Bean.Weather;
import com.example.mymvp_2.MyApplication;
import com.example.mymvp_2.Retrofit02.Modle.Retrofit2Modle;
import com.example.mymvp_2.Retrofit02.View.Retrofit2View;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Retrofit2Presenter extends BasePressenter<Retrofit2View, Retrofit2Modle> {

    public Retrofit2Presenter(Retrofit2View mView) {
        super(mView);
        mModel = new Retrofit2Modle();
    }

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    public void getRetrofit2(){
        mLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        //可在其中解析amapLocation获取相应内容。
                        String city = aMapLocation.getCity();
                        initRecyclerView(city);
                    }
                }
            }
        };
        //初始化定位
        mLocationClient = new AMapLocationClient(MyApplication.context);
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();

        AMapLocationClientOption option = new AMapLocationClientOption();
        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);//签到
        if(null != mLocationClient){
            mLocationClient.setLocationOption(option);
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient.stopLocation();
            mLocationClient.startLocation();
        }
        //设置定位模式为AMapLocationMode.Battery_Saving，低功耗模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);

        //该方法默认为false。
        mLocationOption.setOnceLocation(true);
        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);

        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);

        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(true);

        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);

        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);

        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }
    public void initRecyclerView(String cityName){
        if (cityName.isEmpty()){
            cityName="广州";
        }else {
            cityName= (String) cityName.subSequence(0, cityName.length() - 1);
        }
        Call<Weather> call = mModel.getRetrofit2(cityName);
        call.enqueue(new Callback<Weather>() {
            @Override
            public void onResponse(Call<Weather> call, Response<Weather> response) {
                int code = response.body().getCode();
                if (code == 200) {
                    mView.onRetrofitSuccess(response.body());
                }else {
                    mView.onRetrofitError("错误码："+code);
                }
            }
            @Override
            public void onFailure(Call<Weather> call, Throwable t) {
                mView.onRetrofitError(call.request().url().toString());
            }
        });
    }

    public void Post(){
        Call<String> call = mModel.PostRetrofit();
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                mView.onRetrofitPostSuccess(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }
    public void XMLRetrofit(){
        Call<Channel> channelCall = mModel.XMLRetrofit();
        channelCall.enqueue(new Callback<Channel>() {
            @Override
            public void onResponse(Call<Channel> call, Response<Channel> response) {
                mView.onRetrofitXMLSuccess(response.body());
            }

            @Override
            public void onFailure(Call<Channel> call, Throwable t) {
                Log.e("TAG", "onFailure: "+t);
            }
        });
    }
}
